import js from '@eslint/js';
import eslintPluginImport from 'eslint-plugin-import';
import prettier from 'eslint-plugin-prettier';
import reactHooks from 'eslint-plugin-react-hooks';
import reactRefresh from 'eslint-plugin-react-refresh';
import globals from 'globals';
import tseslint from 'typescript-eslint';

export default tseslint.config(
  { ignores: ['dist', 'node_modules', 'iot', 'src/types/auto-imports.d.ts'] },
  {
    extends: [js.configs.recommended, ...tseslint.configs.recommended],
    files: ['**/*.{ts,tsx}'],
    languageOptions: {
      ecmaVersion: 2020,
      globals: {
        ...globals.browser,
        ...globals.node,
      },
    },
    plugins: {
      'react-hooks': reactHooks,
      'react-refresh': reactRefresh,
      prettier,
      import: eslintPluginImport,
    },
    settings: {
      'import/resolver': {
        node: {
          extensions: ['.js', '.jsx', '.ts', '.tsx'],
          moduleDirectory: ['node_modules', 'src'],
        },
        // 暂时禁用 typescript resolver，因为缺少依赖
        // typescript: {
        //   alwaysTryTypes: true,
        //   project: './tsconfig.app.json',
        // },
      },
      'import/parsers': {
        '@typescript-eslint/parser': ['.ts', '.tsx'],
      },
      'import/extensions': ['.js', '.jsx', '.ts', '.tsx'],
      'import/external-module-folders': ['node_modules', 'node_modules/@types'],
    },
    rules: {
      ...reactHooks.configs.recommended.rules,
      '@typescript-eslint/no-explicit-any': 'warn',
      '@typescript-eslint/no-unused-vars': [
        'warn',
        {
          argsIgnorePattern: '^_',
          varsIgnorePattern: '^_',
          ignoreRestSiblings: true,
        },
      ],
      'react-refresh/only-export-components': ['warn', { allowConstantExport: true }],
      'prettier/prettier': ['error', { singleQuote: true }],
      'react-hooks/exhaustive-deps': 'off',

      // Import 相关规则优化
      'import/no-unresolved': 'off', // 关闭未解析导入检查，因为有自动导入
      'import/no-unused-modules': 'off', // 关闭未使用模块检查
      'import/named': 'off', // 关闭命名导入检查
      'import/default': 'off', // 关闭默认导入检查
      'import/namespace': 'off', // 关闭命名空间导入检查
      'import/no-duplicates': 'off', // 暂时关闭重复导入检查，避免解析器错误
      'import/first': 'off', // 暂时关闭导入顺序检查
      'import/newline-after-import': 'off', // 暂时关闭导入后空行检查

      // 暂时关闭 import/order 规则，避免解析器错误
      'import/order': 'off',
    },
  }
);
